﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;


class ComboboxHelper
{
    public static void RegisterComboBoxAutocomplete(object sender, string[] source)
    {
        if ((sender is System.Windows.Forms.ComboBox && (sender as ComboBox).Text.Length >= 0)
        ||
            (sender is ToolStripComboBox) && (sender as ToolStripComboBox).Text.Length >= 0
            )
        {
            string text = "";
            if (sender is ComboBox)
            {
                text = ((System.Windows.Forms.ComboBox)sender).Text;
            }
            else
            {
                text = ((System.Windows.Forms.ToolStripComboBox)sender).ComboBox.Text;
            }
            if (!string.IsNullOrEmpty(text))
            {
                var query = (from src in source
                             where src.ToLowerInvariant().Contains(text.ToLowerInvariant())
                             select src).Distinct();

                if (query != null)
                {
                    if (query.Count() > 0)
                    {
                        PopulateCombo(sender, query.ToArray());
                        if (sender is ComboBox)
                        {
                            ((System.Windows.Forms.ComboBox)sender).Select(text.Length, 1);
                        }
                        else
                        {
                            ((System.Windows.Forms.ToolStripComboBox)sender).ComboBox.Select(text.Length, 1);
                        }
                    }
                }
            }
        }
    }
    public static void PopulateCombo(object control, string[] p)
    {
        try
        {
            if (control is ComboBox)
            {
                (control as ComboBox).Invoke((MethodInvoker)delegate
                {
                    (control as ComboBox).BeginUpdate();
                    (control as ComboBox).Items.Clear();
                    foreach (string s in p)
                    {
                        (control as ComboBox).Items.Add(s);
                    }
                    (control as ComboBox).EndUpdate();
                });
            }
            else
            {
                (control as ToolStripComboBox).ComboBox.Invoke((MethodInvoker)delegate
                {
                    (control as ToolStripComboBox).ComboBox.BeginUpdate();

                    if ((control as ToolStripComboBox).ComboBox.DataSource == null)
                    {
                        (control as ToolStripComboBox).ComboBox.Items.Clear();
                        foreach (string s in p)
                        {
                            (control as ToolStripComboBox).ComboBox.Items.Add(s);
                        }
                    }
                    else
                    {
                        DataTable dt = ((control as ToolStripComboBox).ComboBox.DataSource as DataTable);
                        dt.DefaultView.RowFilter = "";
                    }
                    (control as ToolStripComboBox).ComboBox.EndUpdate();
                });
            }
            
        }
        catch(Exception ex)
        {
            // swallow
            MessageBox.Show(ex.ToString());
        }
    }
}

